
var oPhone,oPassword,orePassword,oCode,ophCode;


$(".content li input").blur(function(){
	$(this).addClass("input-erro")
})

$(".content li").on("focus","input",function(){
	$(this).removeClass("input-erro");
	$(this).parent().find("i").html("")
})

//手机号验证
function pic1(){
	userPhone = $("#userName").val();
	//console.log(userPhone);
	var  Num = /^1[34578]\d{9}$/;
	if(userPhone == ""){
		$("li:nth-child(2) i").html("请输入手机号");
		$("li:nth-child(2) input").addClass("input-erro");
		$(".content-phoneCode span").click( function(){
			$("li:nth-child(2) i").html("请输入手机号");
		});
	}else if( Num.test(userPhone) ){
		$("li:nth-child(2) input").removeClass("input-erro");
		$(".content-phoneCode span").click( function(){
			$(".imgCode").css({"display":"block"});
			$("li:nth-child(2) i").html("");
		});
		oPhone = true;
	}else{
		$("li:nth-child(2) i").html("手机号格式错误");
		$(".content-phoneCode span").click( function(){
			$("li:nth-child(2) i").html("手机号格式错误");
		});
	}
}
$("#userName").blur(function(){
	pic1();
});

//密码验证
function pic2(){
	userPassword = $("#password").val();
	var Pas = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,32}$/ ;
	if(userPassword == ""){
		$("li:nth-child(5) i").html("请输入登录密码");
	}else if( Pas.test(userPassword) ){
		$("li:nth-child(5) input").removeClass("input-erro");
		oPassword = true;
	}else{
		$("li:nth-child(5) i").html("请输入6-32位字符，需字母数字符号两种以上组合");
	}
}
$("#password").blur(function(){
	pic2();
});

function pic3(){
	rePassword = $("#repeatPassword").val();
	if(rePassword == ""){
		$("li:nth-child(6) i").html("请再次输入登录密码");
	}else if( rePassword != userPassword ){
		
		$("li:nth-child(6) i").html("两次密码不一致，请重新输入");
		
	}else{
		$("li:nth-child(6) input").removeClass("input-erro");
		orePassword = true;
	}
}
$("#repeatPassword").blur(function(){
	pic3();
});


//生成验证码
function fnCheck(){
	var str="";
	for(var i=0;i<4;i++){
		var arr=[];
		arr.push([parseInt(Math.random()*9+1)]);
		
		arr.push(String.fromCharCode(parseInt(Math.random()*26)+65));
		str+=arr[parseInt(Math.random()*2)];
	}
	$(".imgCode b").html(str);
}
fnCheck();
$(".imgCode span").click(fnCheck);


function pic4(){
	userCode = $("#imgCode").val();
	if(userCode == ""){
		$("li:nth-child(3) i").html("请先输入图片验证码");
	}else if(userCode.toLowerCase() == ($(".imgCode b").html()).toLowerCase()){
		$("li:nth-child(3) input").removeClass("input-erro");
		$(".content-phoneCode span").click( function(){
			$(".imgCode").css({"display":"none"});
			$("li:nth-child(2) i").html("验证码已发出，请在10分钟内完成输入");
			$("li:nth-child(4) b").html(parseInt(Math.random()*900000+100000));
		});
		oCode = true;
	}else{
		$("li:nth-child(3) i").html("图片验证码格式错误")
	}
}
$("#imgCode").blur(function(){
	pic4()
})
function pic5(){
	phCode = $("#phoneCode").val();
	if(phCode == ""){
		$("li:nth-child(4) i").html("请输入短信验证码");
	}else if(phCode == $("li:nth-child(4) b").html()){
		$("li:nth-child(4) input").removeClass("input-erro");
		ophCode = true;
	}else{
		$("li:nth-child(4) i").html("请确认短信验证码");
	}
}
$("#phoneCode").blur(function(){
	pic5();
})

//提交

$(".btn").click(function(){
	pic1();
	pic2();
	pic3();
	pic4();
	pic5();
	if(oPhone == true&&oPassword == true&&orePassword == true&&oCode == true&&ophCode == true){
		//alert("确认提交？")
//		var oldCookie = getCookie(userPhone)
//		if(oldCookie){
//			alert("已注册")
//		}else{
//			setCookie(userPhone,{userPhone,userPassword},7)
//			alert("注册成功")
//			location.href = "login.html";
//		}
		
		
		
		var arr = [];
		var json = {
			"name" : userPhone,
			"number":userPassword
		}
		oldCookie = getCookie("userlist");
		console.log(oldCookie);
		var flag = true;
		if(oldCookie.length != 0){
			arr = oldCookie;
			for(var i in arr){
				if(json.name==arr[i].name){
					alert("该账号已注册，请前往登录")
					flag = false;
					location.href = "login.html";
					break;
				}
			}
		}
		if(flag){
			arr.push(json);
			setCookie("userlist",JSON.stringify(arr),7);
			alert("注册成功，请登录")
			location.href = "login.html";
		}
		
	}
})

